package gupao.concurrency.threads;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadSleepDemo {
    static volatile boolean flag = true;

    public static void main(String[] args) throws InterruptedException {
        testSleep();
    }

    private static void testSleep() throws InterruptedException{
        Thread t = new Thread(() -> {
            while (flag) {
                try {
                    Thread.sleep(1000);
                    log.info("sleep 1000ms, {}线程继续继续", Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    log.error("{} 线程被中断，结束运行", Thread.currentThread().getName());
                    flag = false;
                }
            }
        });

        t.start();

        TimeUnit.MILLISECONDS.sleep(5);
        log.info("thread {} status: {}", t.getName(), t.getState());

        TimeUnit.SECONDS.sleep(5);
        t.interrupt();
        TimeUnit.SECONDS.sleep(2);
    }
}
